import 'dart:convert';
import 'dart:typed_data';
import 'dart:ui';
import 'package:flutter_admin/utils/helper/save_file_mobile.dart'
    if (dart.library.html) 'package:flutter_admin/utils/helper/save_file_web.dart';
import 'package:syncfusion_flutter_xlsio/xlsio.dart';

class SaleOrderoutstockExcel {
  static void generateExcel(
      {String? fileName, List? datatable, String? averageValue}) async {
    final Workbook workbook = Workbook();

    final Worksheet sheet = workbook.worksheets[0];
    sheet.enableSheetCalculations();

    List<Map> configure = [
      {
        'name': 'contractid',
        'label': '销售订单号',
        'columnWidth': 22.0,
      },
      {
        'name': 'orderDate',
        'label': '下单日期',
        'columnWidth': 16.0,
      },
      {
        'name': 'outstocktime',
        'label': '发货日期',
        'columnWidth': 16.0,
      },
      {
        'name': 'dayCount',
        'label': '交货周期（天）',
        'columnWidth': 21.0,
      },
      {
        'name': 'productid',
        'label': '产品编码',
        'columnWidth': 20.0,
      },
      {
        'name': 'productname',
        'label': '产品名称',
        'columnWidth': 125.0,
      },
      {
        'name': 'productspec',
        'label': '规格型号',
        'columnWidth': 125.0,
      },
      {
        'name': 'quantity',
        'label': '数量',
        'columnWidth': 10.0,
      },
      {
        'name': 'customername',
        'label': '客户名称',
        'columnWidth': 60.0,
      },
      {
        'name': 'accountingName',
        'label': '产品核算名称',
        'columnWidth': 60.0,
      },
      {
        'name': 'cate',
        'label': '产品种类',
        'columnWidth': 60.0,
      },
    ];
    // 标题
    List titleList = [];
    for (var i = 0; i < configure.length; i++) {
      var title = configure[i];
      sheet.getRangeByIndex(1, i + 1).columnWidth = title["columnWidth"];
      sheet.getRangeByIndex(1, i + 1).rowHeight = 20;
      titleList.add(title["label"]);
    }
    // 添加标题
    sheet.importList(titleList, 1, 1, false);

    for (var i = 0; i < datatable!.length; i++) {
      var text = datatable[i].toMap();
      // 正文
      var textList = [];
      for (var j = 0; j < configure.length; j++) {
        var title = configure[j];
        sheet.getRangeByIndex(i + 1, j + 1).columnWidth = title["columnWidth"];
        sheet.getRangeByIndex(i + 1, j + 1).rowHeight = 20;
        textList.add(text[title["name"]]);
      }
      // 添加正文
      sheet.importList(textList, 2 + i, 1, false);
    }
    // sheet
    //     .getRangeByName('A' +
    //         (datatable.length + 1).toString() +
    //         ':A' +
    //         (datatable.length + 1).toString())
    //     .merge();
    sheet.importList(['平均交货周期（天）：'], datatable.length + 2, 1, false);
    // sheet
    //     .getRangeByName('B' +
    //         (datatable.length + 1).toString() +
    //         ':B' +
    //         (datatable.length + 1).toString())
    //     .merge();
    sheet.importList([averageValue], datatable.length + 2, 2, false);
    final Style globalStyle = workbook.styles.add('globalStyle');
    globalStyle.fontName = 'Times New Roman';
    globalStyle.fontSize = 14;
    globalStyle.backColorRgb = Color.fromARGB(128, 242, 242, 242);
    globalStyle.bold = true;
    globalStyle.wrapText = false;
    globalStyle.hAlign = HAlignType.center;
    globalStyle.vAlign = VAlignType.center;

    final Style globalStyle1 = workbook.styles.add('globalStyle1');
    globalStyle1.fontName = 'Times New Roman';
    globalStyle1.fontSize = 12;
    globalStyle1.wrapText = false;
    globalStyle1.hAlign = HAlignType.center;
    globalStyle1.vAlign = VAlignType.center;

    var char = intToASCII(96 + configure.length);

    sheet.getRangeByName('A1:' + char + '1').cellStyle = globalStyle;

    sheet
        .getRangeByName('A2:' + char + (datatable.length + 2).toString())
        .cellStyle = globalStyle1;

    final List<int>? bytes = workbook.saveAsStream();
    workbook.dispose();

    await FileSaveHelper.saveAndLaunchFile(bytes!, '$fileName.xlsx');
  }

  static dynamic intToASCII(int figure) {
    var listInt = [figure];
    var int2utf8 = Uint8List.fromList(listInt);
    return Utf8Codec().decode(int2utf8);
  }
}
